from flask import Flask, send_from_directory, send_file, request, make_response, jsonify
import os
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

@app.route("/", methods=["GET"])
def getIndex():
    return send_file("static/index.html")

@app.route('/login', methods=['POST'])
def login():
    try:
        if not os.path.exists('userInfo'):
            os.mkdir('userInfo')
        data = request.get_json()
        username = data['username']
        password = data['password']
        with open(os.path.join('userInfo', username + '.txt'), 'w') as file:
            file.write(f"username: {username}\npassword: {password}")
        response = {
            "status": "success",
            "message": "登录成功"
        }
        response = make_response(jsonify(response), 200)
        return response
    except Exception as e:
        print(f"error: {e}")
        response = {
            "status": "error",
            "message": "服务器内部错误"
        }
        response = make_response(jsonify(response), 500)
        return response

if __name__ == '__main__':
    app.run(debug=True, port=8081, host='0.0.0.0')